window.addEventListener("load", getCategory , false);
window.addEventListener("load", getRepeat, false);
window.addEventListener("load", getReminder, false);
window.addEventListener("load",getCategoryListModify,false);
window.addEventListener("load",checkEvent,false);

var ok = 1;
try{
	if (ok == 1)
	var connection = new SQLite("db.sqlite"); 
}catch(err){
	alert("Databa file missing. \n Please at the directory: C:/\Program Files/\Mozilla Thunderbird/\chrome give" +
		" full acces to your user name. ");
	ok = -1;
}

/**
    *@class getFromDb
	*@description The function check if is a event 
	*
    *@author Resz Janos
*/
function checkEvent(){		
   if (ok == -1){
		return;
	} 
	
	var day = new Date();
	var month = day.getMonth()+1;
	try {
		var result = [];
		result = connection.execute(" select Distinct minute"+
									",year,month,"+
									"eventId,categoryId, name,"+
									" day, hour,remindYear,remindMonth,remindDay,"+
									"remindMinute,remindHour,"+
									" description, location,repeatId from"+
									" tb_event where"+ 
									" remindYear < "+day.getFullYear());									
	}catch(err) {
		return;
	}
	if (result.length > 0){
		handleEvent(result);
	}
	
	var day = new Date();
	var month = day.getMonth()+1;
	try {
		var result = [];
	
		result = connection.execute(" select Distinct minute"+
									",year,month,"+
									"eventId,categoryId, name,"+
									" day, hour,remindYear,remindMonth,remindDay,"+
									"remindMinute,remindHour,"+
									" description, location,repeatId from"+
									" tb_event where"+ 
									" remindYear = "+day.getFullYear()+
									" and remindMonth < "+month);									
	}catch(err) {
		return;
	}
	if (result.length > 0){
		handleEvent(result);
	}
	 
	var day = new Date();
	var month = day.getMonth()+1;
	try {
		var result = [];
	
		result = connection.execute(" select Distinct minute"+
									",year,month,"+
									"eventId,categoryId, name,"+
									" day, hour,remindYear,remindMonth,remindDay,"+
									"remindMinute,remindHour,"+
									" description, location,repeatId from"+
									" tb_event where"+ 
									" remindDay < "+day.getDate()+" and remindYear = "+day.getFullYear()+
									" and remindMonth = "+month);									
	}catch(err) {
		return;
	}
	if (result.length > 0){
		handleEvent(result);
	}
	 var day = new Date();
	var month = day.getMonth()+1;
	try {
		var result = [];
	
		result = connection.execute(" select Distinct minute"+
									",year,month,"+
									"eventId,categoryId, name,"+
									" day, hour,remindYear,remindMonth,remindDay,"+
									"remindMinute,remindHour,"+
									" description, location,repeatId from"+
									" tb_event where"+ 
									" remindDay = "+day.getDate()+" and remindYear = "+day.getFullYear()+
									" and remindMonth = "+month+" and remindhour <"+day.getHours());									
	}catch(err) {
		return;
	}
	if (result.length > 0){
		handleEvent(result);
	}
	
	var day = new Date();
	var month = day.getMonth()+1;
	try {
		var result = [];
	
		result = connection.execute(" select Distinct minute"+
									",year,month,"+
									"eventId,categoryId, name,"+
									" day, hour,remindYear,remindMonth,remindDay,"+
									"remindMinute,remindHour,"+
									" description, location,repeatId from"+
									" tb_event where"+ 
									" remindDay = "+day.getDate()+" and remindYear = "+day.getFullYear()+
									" and remindMonth = "+month+" and remindhour = "+day.getHours()+
									" and remindMinute <= "+day.getMinutes());									
	}catch(err) {
		return;
	}
	if (result.length > 0){
		handleEvent(result);
	}
	  
	t = setTimeout('checkEvent()',500);
}
/**
    *@class getFromDb
	*@description The function updatet the remind date for the next remind	
	*
	*@author Resz Janos
	*
	*@param remindYear The year for the next remind date
	*@param remindDay The day for the next remind date
	*@param remindMonth The month for the next remind date
	*@param remindHour The hour for the next remind date
	*@param remindMinute The minute for the next remind date
	*@param eventid The what will be needed to updatet
*/
function setReminderDate(remindYear, remindDay, remindMonth, remindHour,
						 remindMinute, eventId){
	if (ok == -1) {
		return;
	}
	try {
		connection.execute(" update tb_event set remindYear ="+remindYear+
						   " , remindMonth = "+remindMonth+
						   " , remindDay = "+remindDay+
						   " , remindYear = "+remindYear+
						   " , remindHour = "+remindHour+
						   " , remindMinute = "+remindMinute+
						   " where eventId = "+eventId);
	}catch(err) {
		return;
	}
}
/**
	*@class getFromDb
	*@descrption The function write out the events, and calculate the new reminder date
	*
	*@author Resz Janos
	*
	*@param result The list of the events
*/
function handleEvent(result){
	if (ok == -1){
		return;
	}
	for (var i = 0; i<result.length; i++){
		var categoryName = getCategoryNameByIndex(result[i].categoryId)
		var repeatName = 
		month = result[i].remindMonth - 1;
		var repeatName = getRepeatName(result[0].repeatId);
		switch (repeatName.valueOf()){
			case "Daily":
				var remindDate = new Date(result[i].remindYear,month, result[i].remindDay+1, result[i].remindHour, result[i].remindMinute);
				var newRemindDay = remindDate.getDate();
				var newRemindMonth = remindDate.getMonth()+1;
				var newRemindYear = remindDate.getFullYear();
				var newRemindMinute = remindDate.getMinutes();
				var newRemindHour = remindDate.getHours();
			break;	
			case "Weekly":
				var remindDate = new Date(result[i].remindYear,month, result[i].remindDay+7, result[i].remindHour, result[i].remindMinute);
				var newRemindDay = remindDate.getDate();
				var newRemindMonth = remindDate.getMonth()+1;
				var newRemindYear = remindDate.getFullYear();
				var newRemindMinute = remindDate.getMinutes();
				var newRemindHour = remindDate.getHours();
			break;
			case "Yearly":
				var remindDate = new Date(result[i].remindYear+1,month, result[i].remindDay, result[i].remindHour, result[i].remindMinute);
				var newRemindDay = remindDate.getDate();
				var newRemindMonth = remindDate.getMonth()+1;
				var newRemindYear = remindDate.getFullYear();
				var newRemindMinute = remindDate.getMinutes();
				var newRemindHour = remindDate.getHours();
			break;	
			case "Monthly":
				var newRemindDate = new Date(result[i].remindYear,month+1, result[i].remindDay, result[i].remindHour, result[i].remindMinute);
				var newRemindDay = remindDate.getDate();
				var newRemindMonth = remindDate.getMonth()+1;
				var newRemindYear = remindDate.getFullYear();
				var newRemindMinute = remindDate.getMinutes();
				var newRemindHour = remindDate.getHours();
			break;
			default:
				var newRemindDay = 20000;
				var newRemindMonth = 20000;
				var newRemindYear = 20000;
				var newRemindMinute = 20000;
				var newRemindHour = 20000;
			
		}
		setReminderDate(newRemindYear,newRemindDay,newRemindMonth,newRemindHour,
						newRemindMinute,result[i].eventId);
		/*checkEventForm(new String(result[i].name), new String(result[i].description),
					   new String(result[i].hour+":"+result[i].minute),
					   new String(result[i].year+"/"+month+"/"+result[i].month),
					   new String(result[i].categoryName),new String(result[i].location));*/
		month++;		
		alert("Hi, you have an event at the time: "+new String(result[i].year+"/"+month+"/"+
															   result[i].day+" "+
															   hourMinuteToTime(result[i].hour,
																				result[i].minute))+
			 "\n Whit the title: "+new String(result[i].name)+
			 "\n Whit the category: "+ new String(categoryName)+
			 "\n Whit the description: "+new String(result[i].description)+
			 "\n At the location: "+new String(result[i].location));	
		}
	
}
/**
	*@class getFromDb
	*@description The function check if is a event on a date
	*
	*@author Resz Janos
	*
	*@param year The year from the date
	*@param month The month from the date
	*@param day The day from the date
*/
function itsEventAtYearMonthDay(year, month, day) {
	if (ok == -1) {
		return;
	}
	try {
		var result = connection.execute("select name from tb_event where year = "+year+
										" and month = "+month+" and day = "+day);
	}catch(err) {
		return;
	}
	if (result.length > 0){
		return new Number(1);
	}
	return new Number(0);
}
/**
	*@class getFromDb
	*@description The function check if is a event on a date and a hour
	*
	*@author Resz Janos
	*
	*@param year The year from the date
	*@param month The month from the date
	*@param day The day from the date
	*@param hour The hour when I interesting at the event
*/
function itsEventAtYearMonthDayHour(year, month, day, hour) {
	if (ok == -1) {
		return;
	}
	try {
		var result = connection.execute("select name, minute from tb_event where year = "+year+
										" and month = "+month+" and day = "+day+
										" and hour = "+hour);
	}catch(err) {
		return;
	}
	if (result.length > 0){
		return result;
	}
	return new Number(0);
}
/**
	*@class getFromDb
	*@description The funtion check how many events is in a date at a hour
	*
	*@author Resz Janos
	*
	*@param year The year from the date
	*@param month The month from the date
	*@param day The day from the date
	*@param hour The hour when I interesting at the event
*/
function howManyYearMonthDayHour(year, month, day, hour) {
	if (ok == -1) {
		return;
	}
	try {
		var result = connection.execute("select name, minute from tb_event where year = "+year+
										" and month = "+month+" and day = "+day+
										" and hour = "+hour);
	}catch(err) {
		return;
	}
	if (result.length > 0){
		return result.length;
	}
	return new Number(0);
}
/* Get description by day, time, category, title */
/**
	*@class getFromDb
	*@description The function get the descrtion from a event specifacet by a date, time, title
	*
	*@author Resz Janos
	*
	*@param year The year from the date
	*@param month The month from the date
	*@param day The day from the date
	*@param title The title of the event
	*@param hour The hour of the event	
	*@param minute The minute of the event
	*@return Thefucntion return the descrtion of the event
*/
function getDescriptionByYearDayMonthTitleTime(year, month, day, title,  hour, minute) {
	if (ok == -1){
		return;
	}
	try {
		var result = connection.execute("select description from tb_event where year = "+year+
										" and month = "+month+" and day = "+day+
										" and name ='"+title+"'"+
										" and hour = "+hour+" and minute = "+minute);
	}catch(err) {
		return;
	}
	var theDescription = new String(result[0].description);
	return theDescription.valueOf();
}
/**
	*@class getFromDb
	*@description The function get the reminder date time of a event 
	*
	*@author Resz Janos
	*
	*@param year The year from the date
	*@param month The month from the date
	*@param day The day from the date
	*@param hour The hour when I interesting at the event
	*@param minute The minute of the event
	*@param title The title of the event
	*@return The function will return the date time of the event, or --- if the event not will alertet
*/
function getReminderDateByYearDayMonthTitleTime(year, month, day,hour, minute, title) {
	if (ok == -1){
		return;
	}
	try {
		var result = connection.execute("select remindYear, remindMonth,"+ 
										" remindDay, remindHour, remindMinute"+
										" from tb_event where year = "+year+
										" and month = "+month+" and day = "+day+
										" and name ='"+title+"'"+
										" and hour = "+hour+" and minute = "+minute);
	}catch(err) {
		return;
	}
	if (result[0].remindYear == 20000)
		return new String("---");
	var myDate = new Date(result[0].remindYear,result[0].remindMonth-1,result[0].remindDay,
						  result[0].remindHour,result[0].remindMinute);
	var month = myDate.getMonth()+1;
	return new String(myDate.getFullYear()+"/"+month+"/"+myDate.getDate()+" "+
					  hourMinuteToTime(myDate.getHours(),myDate.getMinutes()));
	
}

/* Get location by day, time, category, titlt */
/**
	*@class getFromDb
	*@description The function get the location a event
	*
	*@author Resz Janos
	*
	*@param year The year from the date
	*@param month The month from the date
	*@param day The day from the date
	*@param title The title of the event
	*@param hour The hour when I interesting at the event
	*@param minute The minute of the event
	*@return The function will return the location of the event
*/
function getLocationByYearDayMonthTitleTime(year, month, day, title, hour, minute) {
	if (ok == -1){
		return;
	}
	try {
		var result = connection.execute("select distinct location from tb_event where year = "+year+
										" and month = "+month+" and day = "+day+
										" and name ='"+title+"'"+
										" and hour = "+hour+" and minute = "+minute);
	}catch(err) {
		return;
	}
	var theLocation = new String(result[0].location);
	return theLocation.valueOf();
}
/**
	*@class getFromDb
	*@description The funcion will delete a event
	*
	*@author Resz Janos
	*
	*@param year The year from the date
	*@param month The month from the date
	*@param day The day from the date
	*@param hour The hour when I interesting at the event
	*@param minute The minute of the event
	*@param title The title of the event
*/
function deleteEvent(year, month, day, hour, minute, title) {
	if (ok == -1) {
		return;
	}
	try {
		connection.execute("delete from tb_event where year = "+year+
							" and month = "+month+" and day = "+day+
							" and hour = "+hour+" and minute = "+minute+
							" and name = '"+title+"'");
	}catch(err) {
		return;
	}
}
/**
	*@class getFromDb
	*@descripton The function will return the category of a event
	*
	*@author Resz Janos
	*
	*@param year The year from the date
	*@param month The month from the date
	*@param day The day from the date
	*@param hour The hour when I interesting at the event
	*@param minute The minute of the event
	*@param title The title of the event
*/
function  getCategoryByDateTimeTitle(year, month, day,hour, minute, title){
	if (ok == -1) {
		return;
	}
	try {
		var result = connection.execute("select categoryId from tb_event where year = "+year+
							" and month = "+month+" and day = "+day+
							" and hour = "+hour+" and minute = "+minute+
							" and name = '"+title+"'");
	}catch(err){
		return;
	}
	var index = new Number(result[0].categoryId);
	return getCategoryNameByIndex(index.valueOf());
}
/**
	*@class getFromDb
	*@description The function will modify a event
	*
	*@author Resz Janos
	*
	*@param year The year of the event
	*@param month The month of the event
	*@param day The day of the event
	*@param newYear The new year of event
	*@param newMonth The new month of event
	*@param newDay The new day of event
	*@param name The title of the event
	*@param newName The ne title of the event
	*@param hour The hour when I interesting at the event
	*@param minute The minute of the event
	*@param newHour The new hour of event
	*@param newMinute The minute of the event
	*@param categoryName The caretgory of the event
	*@param newcategoryName The new caretegory of the event
	
*/
function modifyEvent( year, month, day, newYear, newMonth, newDay,
					 name, newName, hour, minute, newHour, newMinute,
					 categoryName, newCategoryName) {
	if (ok == -1){
		return;
	}
	var categoryId = getCategoryIndex(categoryName);
	var newCategoryId = getCategoryIndex(newCategoryName);
	try {
		connection.execute("update tb_event set hour = "+newHour+", minute = "+newMinute+
						   ", year = "+newYear+", month = "+newMonth+", day = "+newDay+
						   ", categoryId = "+newCategoryId+", name = '"+newName+
						   "' where year = "+year+
						   " and month = "+month+" and day = "+day+" and hour = "+hour+
						   " and minute = "+minute+" and categoryId = "+categoryId+
						   " and name = '"+name+"'");
	}catch(err) {
		return;
	}
	
}
/**
	*@class getFromDb
	*@description The function get the of a category
	*
	*@author
	*
	*@param index The id of the category
	*@return The function will return the category name
*/
function getCategoryNameByIndex(index){
	if (ok == -1){
		return;
	}
	try {
		var result = connection.execute("select categoryName from tb_category where categoryId = "+index);
	}catch(err) {
		return;
	}
	return new String(result[0].categoryName);
}
/**
	*@class getFromDB
	*@description The function will insert a new event
	*
	*@author
	*
	*@param category The categoryId of the new event
	*@param reminder The reminderId of the new event
	*@param repeat The repeatId of the new event
	*@param hour The hour of the new event
	*@param minute The minute of the new event
	*@param name The name of the new event
	*@param location The location of the new event
	*@param description The description of the new event
	*@param year The year of the new event
	*@param month The month of the new event
	*@param day The day of the new event
	*@param endYear The end year of the new event
	*@param endMonth The end month of the new event
	*@param endDay The end day of the new event
	*@param endHour The end hour of the new event
	*@param endMinute The end minute of the new event
*/
function newEvent(category, reminder, repeat, hour, minute, 
				  name, location,description ,year, month, day, endYear, endMonth,
				  endDay, endHour, endMinute){
	if (ok == -1) {
		return;
	}
	var categoryId = getCategoryIndex(category);
	var reminderId = getReminderIndex(reminder);
	var repeatId = getRepeatIndex(repeat);
	var description2 = new String(description);
	var location2 = new String(location);
	var reminderNumber = getReminderNumber(reminder);
	switch(reminderNumber.valueOf()) {
		case 15:
			var remindDate = new Date(year,month-1, day, hour, minute-15);
			var remindDay = remindDate.getDate();
			var remindMonth = remindDate.getMonth()+1;
			var remindYear = remindDate.getFullYear();
			var remindMinute = remindDate.getMinutes();;
			var remindHour = remindDate.getHours();
		break;
		case 30:
			var remindDate = new Date(year,month-1, day, hour, minute-30);
			var remindDay = remindDate.getDate();
			var remindMonth = remindDate.getMonth()+1;
			var remindYear = remindDate.getFullYear();
			var remindMinute = remindDate.getMinutes();
			var remindHour = remindDate.getHours();		
		break;
		case 60:
			var remindDate = new Date(year,month-1, day, hour-1, minute);
			var remindDay = remindDate.getDate();
			var remindMonth = remindDate.getMonth()+1;
			var remindYear = remindDate.getFullYear();
			var remindMinute = remindDate.getMinutes();;
			var remindHour = remindDate.getHours();
		break;
		case 1440:
			var remindDate = new Date(year,month-1, day-1, hour, minute);
			var remindDay = remindDate.getDate();
			var remindMonth = remindDate.getMonth()+1;
			var remindYear = remindDate.getFullYear();
			var remindMinute = remindDate.getMinutes();;
			var remindHour = remindDate.getHours();
		break;
		default:
			var remindDate = new Date(year,month-1, day, hour, minute);
			var remindDay = remindDate.getDate();
			var remindMonth = remindDate.getMonth()+1;
			var remindYear = remindDate.getFullYear();
			var remindMinute = remindDate.getMinutes();;
			var remindHour = remindDate.getHours();	
		break;	
	}
	try{		

		connection.execute("INSERT INTO tb_event (categoryId , reminderId, repeatId, hour, minute,name,"+
						   "location, description, year, month, day, endYear, endMonth, endDay, endHour,"+
						   "endMinute,remindYear, remindMonth, remindDay,"+
						   "remindHour, remindMinute) "+
							"VALUES("+categoryId.valueOf()+", "+reminderId.valueOf()+","+
							repeatId.valueOf()+","+hour.valueOf()+", "+minute.valueOf()+
							", '"+name.valueOf()+"', '"+location2.valueOf()+"', '"+
							description2.valueOf()+"', "+year.valueOf()+", "+
							month.valueOf()+", "+day.valueOf()+", "+endYear.valueOf()+
							","+endMonth.valueOf()+", "+endDay.valueOf()+", "+
							endHour.valueOf()+", "+endMinute.valueOf()+","+
							remindYear.valueOf()+","+remindMonth.valueOf()+","+remindDay.valueOf()+
							","+remindHour.valueOf()+","+remindMinute.valueOf()+")");
	}catch(err){
		return -1;
	}		
	return 0;
}
/**
	*@class getFromDb
	*@description The function will get the id of a category by name
	*
	*@author
	*
	*@param categoryName The category name for what I search the Id
	*@return The function will return -1 if no such category in the database or the id of the category
*/
function getCategoryIndex(categoryName){
	try{
		var result = [];
		result = connection.execute("select categoryId from tb_category"+
									" where categoryName = '"+categoryName+"'");
	}catch(err){	
		return;
	}
	if (result.length > 0){
		return new Number(result[0].categoryId);
	}
	return new Number(-1);
}

/**
	*@class getFromDb
	*@description The function will get the id of a reminder by name
	*
	*@author
	*
	*@param remindName The remind name for what I search the Id
	*@return The function will return -1 if no such remind in the database or the id of the remind
*/
function getReminderIndex(remindName){
	try{
		var result = [];
		result = connection.execute("select reminderId from tb_reminder"+
									" where reminderName = '"+remindName+"'");
	}catch(err){	
		return;
	}
	if (result.length > 0){
		return new Number(result[0].reminderId);
	}
	return new Number(-1);
}
/**
	*@class getFromDb
	*@description The function will get the number of a reminder
	*
	*@author Resz Janos
	*
	*@param reminderName The paramater is a name of a reminder
	*@return If no such reminder name the the function will return -1 else will return the number of the remind
*/
function getReminderNumber(reminderName){
	try{
		var result = [];
		result = connection.execute("select remind from tb_reminder"+
									" where reminderName = '"+reminderName+"'");
	}catch(err){	
		return;
	}
	if (result.length > 0){
		return new Number(result[0].remind);
	}
	return new Number(-1);
}
/**
	*@class getFromDb
	*@description The function will get the id of a repeat by name
	*
	*@author
	*
	*@param repeatName The repeat name for what I search the Id
	*@return The function will return -1 if no such repeat in the database or the id of the repeat
*/
function getRepeatIndex(repeatName){
	try{
		var result = [];
		result = connection.execute("select repeatId from tb_repeat"+
									" where repeatName = '"+repeatName+"'");
	}catch(err){	
		return;
	}
	if (result.length > 0){
		return new Number(result[0].repeatId);
	}
	return new Number(-1);
}

/**
	*@class getFromDb
	*@description The function will get repeat name
	*
	*@author
	*
	*@param repeatId The id of the searcher repeat
	*@return The function will return "No repeat" if no such repeat else the name of the repeat
*/
function getRepeatName(repeatId){
	try{
		var result = [];
		result = connection.execute("select repeatName from tb_repeat"+
									" where repeatId = "+repeatId+"");
	}catch(err){	
		return;
	}
	if (result.length > 0){
		return new String(result[0].repeatName);
	}
	return new String("No repeat");
}
/**
	*@class getFromDb
	*@description The function will insert a new category
	*
	*@author
	*
	*@param categoryName The new category name
*/
function newCategory(categoryName){

	if (categoryName.length == 0)
		alert("Please write a category name!");
	else {
		if (ok == -1)
		return;	
		try{
			connection.execute("INSERT INTO tb_category (categoryName) VALUES ('"+categoryName+"')");
		}catch(err){
			return;
		}
	}	
}
/**
	*@class getFromDb
	*@description The function will get all the event
	*
	*@author Resz Janos
	*
	*@return The function will return all the events from the database
*/
function getAllEvent(){
	if (ok == -1)
		return;	
	try{
		
		var result = connection.execute("select categoryName,repeatName, remind, hour, minute, name, "+
															"location, description, year, month, day, endYear, endMonth, endDaY,"+
															"endHour, endMinute from tb_event, tb_category, tb_reminder, tb_repeat"+
															" where tb_event.categoryId = tb_category.categoryId and "+
															"tb_event.repeatId = tb_repeat.repeatId and "+
															"tb_event.reminderId = tb_reminder.reminderId");	
	}catch(err){
		return;
	}
	
	return result;
	
}

function newReminder(remindernr,reminderName){
	if (ok == -1)
	return;	
	try{
		
		connection.execute("INSERT INTO tb_reminder (remind,reminderName) VALUES ("+remindernr+",'"+reminderName+"')");
	}catch(err){
		return;
	}
		
}

/**
	*@class getFromDb
	*@description The functon will get the time of a event specificate by a date and a title
	*
	*@author Resz Janos
	*
	*@param year The year of the event
	*@param month The month of the event
	*@param day The day of the event
	*@param title The title of the event
	*@return The function will return the list of the evesnts time
*/
function getEventTimeByDateTitle(year, month, day, title) { 
	if (ok == -1) {
		return;
	}
	try	{
		var result = connection.execute("select distinct hour,minute from tb_event where month = "+month+
										" and year = "+year+" and day = "+day+" and name='"+title+"'");
	}catch(err) {
		return;
	}
	var time = [];
	for (var i = 0; i < result.length; i++) {
		theHour = new Number(result[i].hour);
		theMinute = new Number(result[i].minute);
		var timee = hourMinuteToTime(theHour, theMinute);
		time.push(timee);	
	}
	return time;
}
/**
	*@class getFromDb
	*@description The functon will get the title of a event specificate by a date
	*
	*@author Resz Janos
	*
	*@param year The year of the event
	*@param month The month of the event
	*@param day The day of the event
	*@return The function will return the list of the evesnts title
*/
function getEventTitleByDate(year, month, day) {
	if (ok == -1) {
		return;
	}
	try {
		var result = connection.execute("select Distinct name from tb_event where month = "+month+
										" and year = "+year+" and day = "+day);
	}catch(err) {
		return;
	}

	var namee = [];
	for (var i = 0; i < result.length; i++) { 
		namee.push(result[i].name);
	}
	
	 return namee;		
}

function getCategory(){
	if (ok == -1)
		return;	
	try{
		var result = connection.execute("select distinct categoryName from tb_category");
	}catch(err){
		deleteTables();
		creatTables();
		initTables();
		return;
	}
   var root = document.getElementById('list1');	
   root.removeAllItems();
   root.setAttribute("label",result[0].categoryName.toString());
	var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","pop1");
   for (var i = 0; i<result.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", result[i].categoryName.toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);
}


function getCategoryListModify() {
	if (ok == -1)
		return;	
	try{
		var result = connection.execute("select distinct categoryName from tb_category");
	}catch(err){
			deleteTables();
		creatTables();
		initTables();
		return;
	}
   
   var root = document.getElementById('categorys');	
   root.removeAllItems();
   root.setAttribute("label",result[0].categoryName.toString());
	var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","poppp");
   for (var i = 0; i<result.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", result[i].categoryName.toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);
}

function getRepeat(){
	if (ok == -1)
		return;
	try{
	var res= connection.execute("select repeatName from tb_repeat");
	}catch(err){
		deleteTables();
		creatTables();
		initTables();		
		return;
	}	
    var root = document.getElementById('list2');	
    root.setAttribute("label",res[0].repeatName.toString());
    var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","pop2");
    for (var i = 0; i<res.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", res[i].repeatName.toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);	
}


function getReminder(){
	if (ok == -1)
		return;
	try{
		var res = connection.execute("select reminderName from tb_reminder");
	}catch(err){
		deleteTables();
		creatTables();
		initTables();
		return;
	}
	
	var root = document.getElementById('list3');	
    root.setAttribute("label",res[0].reminderName.toString());
    var menupopup = document.createElement('menupopup');
	menupopup.setAttribute("id","pop3");
    for (var i = 0; i<res.length; i++) {
	   var menuitem = document.createElement('menuitem');	
	   menuitem.setAttribute("label", res[i].reminderName.toString());
	   menupopup.appendChild(menuitem);
	}
	root.appendChild(menupopup);	
}
